<?php
class Face extends CI_Controller {
    public function __construct() {
        parent::__construct();
        $this->load->model('post_photo_model');
    }
    
    public function wall() {
		$userip = $_SERVER["REMOTE_ADDR"];
		//$userid = ((!isset($_SESSION['userinfo'])) ? -1 : $_SESSION['userinfo']['user_id']);
        //$res = $this->post_photo_model->get_all();
		$res = $this->post_photo_model->get_first_page(15);//TODO:function divide;
		$likeinfo = $this->post_photo_model->get_like_info(100,$userip);
		
		if ($res != NULL) foreach($res as &$photo){
			$photo['if_like'] = 0;
			if ($likeinfo != NULL) foreach($likeinfo as $a){
				if ($photo['photo_id'] == $a['id']){$photo['if_like'] = 1;break;};
				}
		}
		
		if ($res != NULL) {
            $data['photos'] = $res;
        } else {
            $data['photos'] = array();
        }

        $this->load->view('header');
        $this->load->view('navbar');
		
        if ($res != NULL) $this->load->view('facewall', $data);
        $this->load->view('footer');
    }
	
	public function add_like_num() {
		$retJson = array();
		$likephotoid = $this->input->post('photoid');
		$likeuserip = $_SERVER["REMOTE_ADDR"];
		//$likeuserid = $_SESSION['userinfo']['user_id'];
		$data = array(
               'post_photo_id' => $likephotoid,
               'user_id' => ((!isset($_SESSION['userinfo'])) ? -1 : $_SESSION['userinfo']['user_id']),  
               'user_ip' => $likeuserip
			   
            );
					
        $res = $this->post_photo_model->add_like_num($data,$likephotoid);
		if ($res === TRUE) $retJson['code'] = 1;
		else  $retJson['code'] = -1;
		
		$this->output->set_content_type('application/json')->set_output(json_encode($retJson));
		
    }
	
	public function json_post_list() {
        $data = array();
        //$config['total_rows'] = $this->post_model->count_all();
        $config['per_page'] = 15;
		$userip = $_SERVER["REMOTE_ADDR"];
		
        $results = $this->post_photo_model->get_one_page($config['per_page'],$this->input->post('last_id'));
        
		if( $results == NULL ) {
            $data['code'] = -1;
        }
		
		else {
			$data['end'] = $results[(count($results)-1)]['photo_id'];
			$data['code'] = 1;
			$likeinfo = $this->post_photo_model->get_like_info(100,$userip);
			
			foreach($results as &$photo){
				$photo['if_like'] = 0;
				if ($likeinfo != NULL) foreach($likeinfo as $a){
					if ($photo['photo_id'] == $a['id']){$photo['if_like'] = 1;break;};
				}
				
				$str = $photo['location'];
				$type = substr($str,strlen($str)-4);
				if(($type != 'jpeg')&&($type != 'JPEG'))
			$str='upload/thumbs/'.substr($str,7,strlen($str)-11).'_thumb'.$type;
		else if  ($type == 'jpeg') 
			$str='upload/thumbs/'.substr($str,7,strlen($str)-12).'_thumb.jpeg';
		else $str='upload/thumbs/'.substr($str,7,strlen($str)-12).'_thumb.JPEG';
		if (file_exists($str))
			$photo['location']= $str;
		
		
			}
		
		shuffle($results);
		
		$data['results'] = $results;		
		}
		
        $this->output->set_content_type('application/json')
        ->set_output(json_encode($data));        
    }
	
	
}
?>